#include "mycoin.h"
#include <QDebug>

//MyCoin::MyCoin(QWidget *parent) : QWidget(parent)
//{

//}

MyCoin::MyCoin(QString butimg){
    QPixmap pix;
    bool ret=pix.load(butimg);
    if(!ret){
        qDebug()<<butimg<<"加载图片失败";
    }

    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    //初始化定时器
    timer1=new QTimer(this);
    timer2=new QTimer(this);

    //监听正面翻转的信号槽
    connect(timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg(this->min++);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        //如果大于最大值，重置最小值，并停止定时器
        if(this->min>this->max){
            this->min=1;
            this->isAnimation=false;
            timer1->stop();
        }
    });

    connect(timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/res/Coin000%1.png").arg((this->max)--);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        //如果小于最小值，重置最大值，并停止定时器
        if(this->max<this->min){
            this->max=8;
            this->isAnimation=false;
            timer2->stop();
        }
    });
}

void MyCoin::changeFlag(){
    //如果是正面，执行下列代码
    if(this->flag){
        timer1->start(30);
        this->isAnimation=true;
        this->flag=false;
    }else{
        //反面执行下列代码
        timer2->start(30);
        this->isAnimation=true;
        this->flag=true;
    }
}

void MyCoin::mousePressEvent(QMouseEvent *e){
    if(this->isAnimation || this->isWin){
        return;
    }else{
        return QPushButton::mousePressEvent(e);
    }
}
